'use strict';
app.controller('bulletinBoardCtrl',['$scope','utilService','$modal', function($scope, utilService, $modal){
    $scope.bulletinBoard = {};
    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domainAgent + 'manage/bulletinBoard/getBulletinBoardList',
            params:$scope.bulletinBoard
        }).then(function(result){});
    }
    $scope.query();

    //发布公告
    $scope.openAdd = function () {
        $modal.open({
            templateUrl: 'tpl/bulletin_board/bulletin_board_add.html',
            controller: 'bulletinBoardAddCtrl',
            scope:$scope,
            resolve: {
                items: function () {
                    return  $scope;
                }
            }
        });
    };

    // 删除公告信息
    $scope.openDelete = function(record) {
        $scope.modal= {}
        $scope.modal.title = "删除公告";
        $scope.modal.message = "确定删除'"+ record.title +"'公告？";
        utilService.modal($modal, $scope, {
            title: $scope.modal.title,
            controller: "bulletinBoardDeleteCtrl",
            message: $scope.modal.message,
            data:{
                id: record.id
            }
        });
    }

    // 编辑
    $scope.openEdit = function (bulletinBoard) {
        $modal.open({
            templateUrl: 'tpl/bulletin_board/bulletin_board_edit.html',
            controller: 'bulletinBoardEditCtrl',
            scope:$scope,
            resolve: {
                bulletinBoard: function() {
                    return bulletinBoard
                }
            }
        });
    };
}]);


app.controller('bulletinBoardAddCtrl', ['$scope', '$modalInstance', 'bulletinBoardService', 'toaster',
    function($scope, $modalInstance, bulletinBoardService, toaster) {

        $scope.bulletinBoard = {
            title: '',
            content: '',
            level: -1,
            agentId: ''
        }

        $scope.agentList = {};
        bulletinBoardService.allAgentList($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.agentList = result.data;
                    break;
                default :
                    toaster.pop('error', "提示", result.message);
                    break;
            }
        });

        $scope.submit = function (form) {
            if(!form.$valid) {
                // 表单验证没通过
                return;
            }

            if($scope.bulletinBoard.level == -2
                && $scope.bulletinBoard.agentId == '') {
                toaster.pop('warning', "提示", '请选择指定代理商进行收信');
                return;
            }

            bulletinBoardService.add($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "提示", '发布成功');
                        $scope.cancel();
                        $scope.query();
                        break;
                    default :
                        toaster.pop('error', "提示", result.message);
                        break;
                }
            });
        }

        $scope.$watch('bulletinBoard.level',
            function (newVal, oldVal) {
                $scope.bulletinBoard.agentId = '';
            }
        )

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);


app.controller('bulletinBoardEditCtrl', ['$scope', '$modalInstance', 'bulletinBoardService', 'toaster', 'bulletinBoard',
    function($scope, $modalInstance, bulletinBoardService, toaster, bulletinBoard) {

        $scope.bulletinBoard = {
            id: bulletinBoard.id,
            title: bulletinBoard.title,
            content: bulletinBoard.content,
            level: bulletinBoard.agentId != '' ? -2 : bulletinBoard.level,
            agentId: ''
        };

        $scope.agentList = {};
        bulletinBoardService.allAgentList($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.agentList = result.data;
                    $scope.bulletinBoard.agentId = bulletinBoard.agentId;
                    break;
                default :
                    toaster.pop('error', "提示", result.message);
                    break;
            }
        });

        $scope.submit = function (form) {
            if(!form.$valid) {
                // 表单验证没通过
                return;
            }

            if($scope.bulletinBoard.level == -2
                && $scope.bulletinBoard.agentId == '') {
                toaster.pop('warning', "提示", '请选择指定代理商进行收信');
                return;
            }

            bulletinBoardService.update($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "提示", '更改成功');
                        $scope.cancel();
                        $scope.query();
                        break;
                    default :
                        toaster.pop('error', "提示", result.message);
                        break;
                }
            });
        }

        $scope.$watch('bulletinBoard.level',
            function (newVal, oldVal) {
                $scope.bulletinBoard.agentId = '';
            }
        )

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);


app.controller('bulletinBoardDeleteCtrl', ['$scope', '$modalInstance', 'bulletinBoardService', 'toaster', 'modalData',
    function($scope, $modalInstance, bulletinBoardService, toaster, modalData) {
        var id = modalData.data.id;

        $scope.isAllowSubmit=true;
        $scope.ok = function(){
            if(!$scope.isAllowSubmit){
                return;
            }
            $scope.isAllowSubmit = false;
            bulletinBoardService.delete($scope, id).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.query();
                        toaster.pop('success', "提示", "删除成功");
                        break;
                    default :
                        toaster.pop('error', "提示", result.message);
                        break;
                }
                $scope.cancel();
                $scope.isAllowSubmit=true;
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);